import pandas as pd
import numpy as np
import json
import os
from typing import Dict, List, Any, Tuple


def load_hardware_data(data_path: str) -> pd.DataFrame:
    """
    加载硬件诊断数据
    
    Args:
        data_path: 数据文件路径
    
    Returns:
        处理后的DataFrame
    """
    try:
        df = pd.read_csv(data_path)
        return df
    except Exception as e:
        print(f"加载硬件数据失败: {e}")
        return pd.DataFrame()


def load_software_data(data_path: str) -> pd.DataFrame:
    """
    加载软件异常数据
    
    Args:
        data_path: 数据文件路径
    
    Returns:
        处理后的DataFrame
    """
    try:
        df = pd.read_csv(data_path)
        return df
    except Exception as e:
        print(f"加载软件数据失败: {e}")
        return pd.DataFrame()


def load_ticket_data(data_path: str) -> pd.DataFrame:
    """
    加载工单数据
    
    Args:
        data_path: 数据文件路径
    
    Returns:
        处理后的DataFrame
    """
    try:
        df = pd.read_csv(data_path)
        return df
    except Exception as e:
        print(f"加载工单数据失败: {e}")
        return pd.DataFrame()


def process_hardware_features(df: pd.DataFrame) -> pd.DataFrame:
    """
    处理硬件特征数据
    
    Args:
        df: 原始DataFrame
    
    Returns:
        处理后的DataFrame
    """
    # 填充缺失值
    df = df.fillna({
        'avg_voltage': df['avg_voltage'].median(),
        'voltage_drop_rate': df['voltage_drop_rate'].median(),
        'signal_strength_avg': df['signal_strength_avg'].median(),
        'screen_refresh_count': 0
    })
    
    # 处理温度统计数据（如果是JSON格式）
    if 'temperature_stats' in df.columns:
        if isinstance(df['temperature_stats'].iloc[0], str):
            temp_stats = df['temperature_stats'].apply(lambda x: json.loads(x) if pd.notna(x) else {})
            df['temp_max'] = temp_stats.apply(lambda x: x.get('max', df['avg_voltage'].median()))
            df['temp_min'] = temp_stats.apply(lambda x: x.get('min', df['avg_voltage'].median()))
            df['temp_var'] = temp_stats.apply(lambda x: x.get('var', 0))
            df.drop('temperature_stats', axis=1, inplace=True)
    
    # 固件版本编码
    if 'firmware_version' in df.columns:
        df = pd.get_dummies(df, columns=['firmware_version'], prefix='fw')
    
    # 位置分组编码
    if 'location_cluster' in df.columns:
        df = pd.get_dummies(df, columns=['location_cluster'], prefix='loc')
    
    return df


def process_software_features(df: pd.DataFrame) -> pd.DataFrame:
    """
    处理软件特征数据
    
    Args:
        df: 原始DataFrame
    
    Returns:
        处理后的DataFrame
    """
    # 填充缺失值
    df = df.fillna({
        'heartbeat_interval_std': 0,
        'reboot_count': 0,
        'network_retry_ratio': 0,
        'log_entropy': 0
    })
    
    # 处理错误码分布（如果是JSON格式）
    if 'error_code_dist' in df.columns:
        if isinstance(df['error_code_dist'].iloc[0], str):
            error_dists = df['error_code_dist'].apply(lambda x: json.loads(x) if pd.notna(x) else {})
            # 提取常见错误码
            common_errors = ['NET_TIMEOUT', 'MEM_OVERFLOW', 'IO_ERROR', 'SENSOR_FAIL']
            for error in common_errors:
                df[f'error_{error}'] = error_dists.apply(lambda x: x.get(error, 0))
            df.drop('error_code_dist', axis=1, inplace=True)
    
    return df


def process_ticket_features(df: pd.DataFrame) -> pd.DataFrame:
    """
    处理工单特征数据
    
    Args:
        df: 原始DataFrame
    
    Returns:
        处理后的DataFrame
    """
    # 填充缺失值
    df = df.fillna({'text_keywords': '{}'})
    
    # 处理关键词数据（如果是JSON格式）
    if 'text_keywords' in df.columns:
        if isinstance(df['text_keywords'].iloc[0], str):
            keywords_data = df['text_keywords'].apply(lambda x: json.loads(x) if pd.notna(x) else {})
            # 提取常见关键词
            common_keywords = ['刷新慢', '黑屏', '电池', '联网', '闪退', '发热']
            for keyword in common_keywords:
                df[f'keyword_{keyword}'] = keywords_data.apply(lambda x: x.get(keyword, 0))
            df.drop('text_keywords', axis=1, inplace=True)
    
    # 客户类型编码
    if 'customer_segment' in df.columns:
        df = pd.get_dummies(df, columns=['customer_segment'], prefix='cust')
    
    # 设备年龄段编码
    if 'device_age_bucket' in df.columns:
        df = pd.get_dummies(df, columns=['device_age_bucket'], prefix='age')
    
    return df


def split_train_test(X: pd.DataFrame, y: pd.Series, test_size: float = 0.2, random_state: int = 42) -> Tuple[pd.DataFrame, pd.DataFrame, pd.Series, pd.Series]:
    """
    划分训练集和测试集
    
    Args:
        X: 特征数据
        y: 标签数据
        test_size: 测试集比例
        random_state: 随机种子
    
    Returns:
        (X_train, X_test, y_train, y_test)
    """
    from sklearn.model_selection import train_test_split
    return train_test_split(X, y, test_size=test_size, random_state=random_state)


def save_processed_data(df: pd.DataFrame, output_path: str) -> None:
    """
    保存处理后的数据
    
    Args:
        df: 处理后的DataFrame
        output_path: 输出文件路径
    """
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    df.to_csv(output_path, index=False)
    print(f"处理后的数据已保存至: {output_path}")
